{% extends "master.html" %}
{% if obj.direction == IVTNT_IN %}
    {% set label = '入库' %}
{% elif obj.direction == IVTNT_OUT %}
    {% set label = '出库' %}
{% elif obj.direction == IVTNT_INTERNAL %}
    {% set label = '内部转仓' %}
{% endif %}

{% block subtitle %} - 出入库管理 - {{label}}单详情{% endblock %}


{% block extCSS %}

<style type="text/css">    
    #appModal{
        width : 730px;
        margin-left: -360px;
    }
    
    #itemModal{
        width : 730px;
        margin-left: -360px;
    }
</style>
{% endblock %}


{% block extJS %}

<script language="JavaScript" type="text/javascript">
//<![CDATA[
    function toApprove(){
       var flag = true;
       $(".err").removeClass("err")
       $("select[name^='ivt_']").each(function(){
           var t = $(this);
           if(!t.val()){
               t.addClass("err");
               flag = false;
           }
       })
       if(!flag){
           return false;
       }else{
           $("#appForm").submit();
       }
    }

    function tracItem(no){
        var url = "{{url_for('bpTrc.view',action='ajaxTrc')}}";
        var params = {'no' : no, 't' : nowstr() }
        $.getJSON(url,params,function(r){
            if(r.code != 0 ){
                alert(r.msg);
            }else{
                var html = '';
                for(var i=0;i<r.data.length;i++){
                    var d = r.data[i];
                    html += '<tr>';
                    html += '<td>'+d.appTime+'</td>';
                    html += '<td>'+d.createBy+'</td>';
                    html += '<td>'+d.refer+'</td>';
                    html += '<td>'+d.event+'</td>';
                    html += '</tr>';
                }
                $('#itemLog').html(html);
                $("#itemModal").modal('show');
            }
        })
    }
    
    function toReject(){
        var url = "{{url_for('bpIvtnt.view',action='disapp',id=obj.id)}}";
        {% if obj.direction == IVTNT_IN %}
            var msg = "确定拒绝收货并且退回？";
        {% else %}
            var msg = "确定取消发货？";
        {% endif %}
        redirect_alert(msg,url);
    }
       
    function toWastage(){
       var url = "{{url_for('bpIvtnt.view',action='turnToWastage',id=obj.id)}}";
       var warning = "该操作会把当前该表单中的商品标记为损耗，所相关的商品个体不可再被使用，只适用于商品丢失，损坏等情况。此操作是不可逆操作，你确定要继续吗？";
       
       var msg = [];
       clearMsg();
       
       var totalQty = 0;
       var total = 0;
        $(".qtytd").each(function(){
            var t= $(this);
            total += parseInt(t.text());
        });
       
       var checkedItem = {{obj.itemList|list|length}};
       var total = {{obj.dtls|sum('qty')}};
       
       if( total != checkedItem){
           msg.push("相关联的个体商品的数量与商品数量不相符，请先编辑！");
       }
       
       if(!$("#remark").text().trim()){
           msg.push("请编辑该表单并于‘备注’填写损耗的原因，再进行该操作！");
       }       
       
       if(msg.length > 0){
            alertError(msg);
            return false;
        }else{
           redirect_alert(warning,url);
        }
        
    }
//]]>
</script>
{% endblock %}



{% block main %}
<div class="container">
    <div class="row">
        <ol class="breadcrumb">
          <li><a href="{{url_for('bpRoot.view')}}">主面板</a></li>
          <li><a href="{{url_for('bpIvtnt.view')}}">出入库管理</a></li>
          <li class="active">{{label}}单</li>
        </ol>
    </div>
    <div class="row">
        <ul class="nav nav-tabs">
          <li class="active"><a href="#">{{label}}单详情</a></li>
          <li><a href="{{url_for('.view',action='viewLog',id=obj.id)}}">日志记录</a></li>
        </ul>
    </div>
    <div class="row">
        <div class="btn-toolbar pull-right">
            {% if obj.editable() %}
                <div class="btn-group"><a class="btn btn-default" href="{{url_for('.view',action='update',id=obj.id)}}">{{BTN_UPDATE}}</a></div>
                {% if obj.splitable() %}
                    <div class="btn-group"><a class="btn btn-info" href="{{url_for('.view',action='split',id=obj.id)}}">分拆</a></div>
                {% endif %}            
            {% endif %}
            {% if obj.approvable() %}
                
                {% if obj.direction == IVTNT_IN %}
                    <div class="btn-group"><a class="btn btn-primary" data-toggle="modal" href="#appModal">确认收货</a></div>
                    {% if canReject %}
                        <div class="btn-group"><a class="btn btn-inverse" href="#" onclick="toReject()">拒绝并退还</a></div>
                    {% endif %}
                    <div class="btn-group"><a class="btn btn-danger" href="#" onclick="toWastage()">转为损耗</a></div>
                {% elif  obj.direction == IVTNT_OUT %}
                    <div class="btn-group"><a class="btn btn-primary" href="{{url_for('.view',action='app',flag=IVTNT_APPROVE,id=obj.id)}}">确认发货</a></div>
                    <div class="btn-group"><a class="btn btn-danger" href="#" onclick="toReject()">取消发货</a></div>
                {% endif %}
                <!-- 
                <div class="btn-group"><a class="btn btn-primary" href="{{url_for('.view',action='app',flag=IVTNT_DISAPPROVE,id=obj.id)}}">{{BTN_REJECT}}</a></div>
                    -->
            {% endif %}
        </div>
    </div>
    <div class="row">
        <fieldset>
            <legend>{{label}}单详情</legend>
            <div class="col-md-4">
                <h4>系统编号</h4><p>{{obj.no}}</p>
                
                {% if obj.direction == IVTNT_IN %}
                    <h4>从&nbsp;[{{obj.ivt.name|ss(c='IVTNAME')}}]&nbsp;入库</h4><p>&nbsp;</p>                
                {% elif  obj.direction == IVTNT_OUT %}
                    <h4>出库到&nbsp;[{{obj.ivt.name|ss(c='IVTNAME')}}]</h4><p>&nbsp;</p>
                {% endif %}
                
                <h4>涉及</h4>
                <p>
                    {{ mymacros.render_goto_popup(obj.refer) }}
                </p>
            </div>
            <div class="col-md-4">
                <h4>经办人</h4><p>{{obj.createBy|f}}&nbsp;</p>
                <h4>开单时间</h4><p>{{obj.createTime|fd}}</p>
            </div>
            <div class="col-md-4">
                <h4>备注</h4><p id="remark">{{obj.remark|f}}&nbsp;</p>
                <h4>状态</h4><p>{{obj.status|ss(c='IVTNT')}}&nbsp;</p>
            </div>
            <div class="col-md-12">
             <h4>附件</h4>
                {{mymacros.render_preview_all_attachment(obj.attachment)}}
            </div>
            <div class="col-md-12 clearfix">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <td class="col-md-3">商品编号</td>
                            <td class="col-md-2">商品名称</td>
                            <td class="col-md-3">商品描述</td>
                            {% if obj.direction == IVTNT_IN %}
                                <td  class="col-md-2">入库仓位</td>
                            {% elif obj.direction == IVTNT_OUT %}
                                <td class="col-md-2">所在仓位</td>
                            {% endif %}
                            <td class="col-md-1 qtytd">{{label}}数量</td>
                            <td>备注</td>      
                        </tr>
                    </thead>
                    <tbody>
                        {% for r in obj.dtls %}
                            <tr>
                                <td>{{mymacros.render_goto_popup_pdt(r.pdt, loop.first)}}</td>
                                <td>{{r.pdtname|f}}</td>
                                <td>{{r.pdtfulldesc|f}}</td>
                                {% if obj.direction == IVTNT_IN %}
                                    <td>{{r.dIvtLtn|f}}</td>
                                {% elif obj.direction == IVTNT_OUT %}
                                    <td>{{r.sIvtLtn|f}}</td>
                                {% endif %}
                                <td>
                                    <div class="btn-group">
                                      <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
                                        {{r.qty}}
                                        <span class="caret"></span>
                                      </a>
                                      <ul class="dropdown-menu">
                                          <li>&nbsp;相关的商品个体</li>
                                          <li class="divider"></li>
                                           {% for no in r.itemList %}
                                                <li>&nbsp;{{no}}</li>
                                            {% endfor %}
                                      </ul>
                                    </div> 
                                </td>
                                <td>{{r.remark|f}}&nbsp;</td>
                            </tr>
                        {% endfor %}
                    </tbody>                
                </table>
            </div>
        </fieldset>
        {{mymacros.render_system_info(obj)}}
    </div>
    
</div>


{% if obj.direction == IVTNT_IN and obj.approvable() %}
<!-- modal begin -->
<div id="appModal" class="modal hide fade" tabindex="-1" data-width="560" aria-hidden="true" style="display: none;">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    请选择收货仓位
  </div>
  <div class="modal-body">
    <form id="appForm" method='POST' action="{{url_for('.view',action='app')}}">
        <input type="hidden" name="flag" value="{{IVTNT_APPROVE}}"/>
        <input type="hidden" name="id" value="{{obj.id}}"/>
    {% for r in obj.dtls %}
        <div class="row">
            <div class="col-md-6">{{r.pdt.name}}</div>
            <div class="col-md-6">
                <select name="ivt_{{r.id}}">
                    <option value=""></option>
                    {% for ivt in ivts %}
                        <option value="{{ivt.id}}">{{ivt}}</option>
                    {% endfor %}
                </select>
            </div>
        </div>
    {% endfor %}
    </form>
  </div>
  <div class="modal-footer">
    <button type="button" data-dismiss="modal" class="btn btn-default">{{BTN_CLOSE}}</button>
    <button type="button" class="btn btn-primary" onclick="toApprove()">{{BTN_SUBMIT}}</button>
  </div>
</div>
<!-- modal end -->
{% endif %}





<!-- modal begin -->
<div id="itemModal" class="modal hide fade" tabindex="-1" aria-hidden="true" style="display: none;">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    单个物品追踪记录
  </div>
  <div class="modal-body">
    <div class="row">
        <div class="col-md-9">
            
        <table class="table table-bordered">
            <thead>
                <tr>
                    <td class="col-md-2">发生时间</td>
                    <td class="col-md-1">经办人</td>
                    <td class="col-md-2">涉及</td>
                    <td>事件</td>
                </tr>
            </thead>
            <tbody id="itemLog">
                
            </tbody>
        </table>
        </div>
    </div>
  </div>
  <div class="modal-footer">
    <button type="button" data-dismiss="modal" class="btn btn-default">{{BTN_CLOSE}}</button>
  </div>
</div>
<!-- modal end -->





{% endblock %}